package com.stationbuilding.adapter.quality;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.stationbuilding.R;
import com.stationbuilding.bean.DictBean;
import com.stationbuilding.bean.FileBean;
import com.stationbuilding.bean.QuestionBean;
import com.stationbuilding.constants.Constants;
import com.stationbuilding.utils.DateUtils;

import java.util.List;

public class QualityFragmentHomeListAdapter extends BaseExpandableListAdapter {

    private Context mContext;
    private List<DictBean> groupData;
    private List<QuestionBean> itemData;

    public QualityFragmentHomeListAdapter(Context mContext, List<DictBean> groupData, List<QuestionBean> itemData) {
        this.mContext = mContext;
        this.groupData = groupData;
        this.itemData = itemData;
    }

    public void setGroupData(List<DictBean> groupData) {
        this.groupData = groupData;
    }

    public void setItemData(List<QuestionBean> itemData) {
        this.itemData = itemData;
    }

    @Override
    public int getGroupCount() {
        return groupData == null ? 0 : groupData.size();
    }

    @Override
    public int getChildrenCount(int groupPosition) {
        return itemData == null ? 0 : itemData.size();
    }

    @Override
    public Object getGroup(int groupPosition) {
        return groupData == null ? null : groupData.get(groupPosition);
    }

    @Override
    public Object getChild(int groupPosition, int childPosition) {
        return itemData == null ? null : itemData.get(childPosition);
    }

    @Override
    public long getGroupId(int groupPosition) {
        return groupPosition;
    }

    @Override
    public long getChildId(int groupPosition, int childPosition) {
        return childPosition;
    }

    @Override
    public boolean hasStableIds() {
        return false;
    }

    @Override
    public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
        ViewHolderGroup groupHolder;
        if(convertView == null){
            convertView = LayoutInflater.from(mContext).inflate(R.layout.home_group, parent, false);
            groupHolder = new ViewHolderGroup();
            groupHolder.tv_status = (TextView) convertView.findViewById(R.id.status);
            groupHolder.iv_group_icon = (ImageView) convertView.findViewById(R.id.group_icon);
            convertView.setTag(groupHolder);
        }else{
            groupHolder = (ViewHolderGroup) convertView.getTag();
        }
        groupHolder.tv_status.setText(groupData.get(groupPosition).getName());
        if (isExpanded) {
            groupHolder.iv_group_icon.setImageResource(R.drawable.ic_expand_less);
        } else {
            groupHolder.iv_group_icon.setImageResource(R.drawable.ic_expand_more);
        }
        return convertView;
    }

    @Override
    public View getChildView(int groupPosition, final int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
        ViewHolderItem itemHolder;
        final QuestionBean questionBean = itemData.get(childPosition);
        if(convertView == null){
            convertView = LayoutInflater.from(mContext).inflate(R.layout.home_item, parent, false);
            itemHolder = new ViewHolderItem();
//            itemHolder.img_icon = (ImageView) convertView.findViewById(R.id.img);
//            itemHolder.img_video = (ImageView) convertView.findViewById(R.id.video);
            itemHolder.tv_title = (TextView) convertView.findViewById(R.id.title);
//            itemHolder.tv_initiator = (TextView) convertView.findViewById(R.id.initiator);
//            itemHolder.tv_startDate = (TextView) convertView.findViewById(R.id.updateDate);
            itemHolder.tv_warning = (TextView) convertView.findViewById(R.id.status);
            convertView.setTag(itemHolder);
            convertView.setTag(R.layout.home_item, childPosition);
        }else{
            itemHolder = (ViewHolderItem) convertView.getTag();
        }
        List<FileBean> imageList = questionBean.getFileList();
        if (imageList != null && imageList.size() > 0) {
            String fileType = imageList.get(0).getDocMold();
            if (Constants.IMAGE_TYPE_LIST.contains(fileType)) {
                Glide.with(mContext).load(Constants.BASE_URL + imageList.get(0).getDocPath().replaceAll("\\\\", "/")).into(itemHolder.img_icon);
                itemHolder.img_video.setVisibility(View.GONE);
            } else if (Constants.VIDEO_TYPE_LIST.contains(fileType)) {
                String[] paths = imageList.get(0).getDocPath().split(",");
                Glide.with(mContext).load(Constants.BASE_URL + paths[0].replaceAll("\\\\", "/")).into(itemHolder.img_icon);
                itemHolder.img_video.setVisibility(View.VISIBLE);
            } else {
                Glide.with(mContext).load(R.drawable.default_image).into(itemHolder.img_icon);
                itemHolder.img_video.setVisibility(View.GONE);
            }
        } else {
            Glide.with(mContext).load(R.drawable.default_image).into(itemHolder.img_icon);
            itemHolder.img_video.setVisibility(View.GONE);
        }
        itemHolder.tv_title.setText(questionBean.getQuestionName());
        itemHolder.tv_initiator.setText(questionBean.getInitiatorName());
        itemHolder.tv_startDate.setText(questionBean.getStartDate().substring(0, 10));
        if (Constants.PROGRESS_STATUS_3.equals(questionBean.getProcessStatus())) {
            int days = DateUtils.differentDaysToCurrentDate(questionBean.getModificationDate());
            itemHolder.tv_warning.setText(String.valueOf(days));
            itemHolder.tv_warning.setVisibility(View.VISIBLE);
            if (days > Constants.WARNING_DAYS_RED) {
                itemHolder.tv_warning.setBackgroundResource(R.drawable.warning_red_text_view_border);
            } else if (days > Constants.WARNING_DAYS_ORANGE) {
                itemHolder.tv_warning.setBackgroundResource(R.drawable.warning_orange_text_view_border);
            } else {
                itemHolder.tv_warning.setBackgroundResource(R.drawable.warning_yellow_text_view_border);
            }
        } else {
            itemHolder.tv_warning.setVisibility(View.GONE);
        }
        return convertView;
    }

    @Override
    public boolean isChildSelectable(int groupPosition, int childPosition) {
        return true;
    }

    private static class ViewHolderGroup{
        private TextView tv_status;
        private ImageView iv_group_icon;
    }

    private static class ViewHolderItem{
        private ImageView img_icon;
        private ImageView img_video;
        private TextView tv_title;
        private TextView tv_initiator;
        private TextView tv_startDate;
        private TextView tv_warning;
    }
}
